class CommentsController < ApplicationController
  before_filter :authorize  

  active_scaffold :comment do |config|
      config.label = "Comments"
      config.columns = [:title,:body,:aliasn,:user,:created_at,:ispublished]
      config.create.columns.exclude (:user)
      config.update.columns.exclude (:user)
      config.show.columns.exclude (:user)
      
    config.columns[:ispublished].label = 'P'	      
      
   #    	  config.columns[:ispublished].inplace_edit = true
   #       config.columns[:ispublished].form_ui = :checkbox  
           	  	     config.columns[:body].form_ui = :textarea
    config.columns[:body].options = {:rows => 60, :cols => 100}
  end
  
  def submpostartic
		  @commobj = Comment.find(params[:id])
		  @commobj.ispublished = true
		  @commobj.save  
		  
    begin     
    	 @articobj = Article.find(@commobj.article_id)
      rescue ActiveRecord::RecordNotFound
        @articobj = nil
    end		  

		  
		  if @articobj.nil?
		  		   @artictitle = ""

		  else
		   @artictitle = @articobj.title
		  end			
		  
		  begin
		   @photouobj = Photo.find(:first,:conditions=>["photos.ualia_id = ?",(Ualia.find_by_name(@articobj.user_published_by)).id])		  	     
      rescue ActiveRecord::RecordNotFound
        @photouobj = nil
      end		
      
      if @photouobj.nil?
		   @photouname = ""
		  else
		   @photouname = "http://www.click2edit.com"+@photouobj.public_filename
		  end		  
		  
		  begin
		   @ualiaobj = Ualia.find_by_name(@commobj.aliasn)
		  rescue ActiveRecord::RecordNotFound
        @ualiaobj = nil
      end	
      
		  if @ualiaobj.nil?
		   @uprofiled = ""
		   @uemail = ""
		  else
		   @uprofiled = @ualiaobj.profile
		   if @uprofiled.nil?
		    @uprofiled = ""
		   end
		   @uemail = @ualiaobj.email_addr
		   if @uemail.nil?
		    @uemail = ""
		   end
		  end 		     

    @wsurln = Websitesinf.find(@articobj.websitesinf_id).url1		  
    s_wsaddr = ""    
      case @wsurln.downcase
        when "www.deathtoglamour.com" : 
              s_wsaddr = "http://84.234.17.41:3009/"+"backend/api"
                                             
        else 
              s_wsaddr = ""
        
      end 		  
    if s_wsaddr != ""  
     blog = ActionWebService::Client::Soap.new(ArticleApi, s_wsaddr)
 		 blog.ins_comm(@artictitle,@commobj.body,@commobj.aliasn,@uemail,@photouname,@uprofiled)  	
 		end	  
		   
		
  end
  
	def before_update_save(record); 
	
		  @commobj = Comment.find(record.id)
		 begin     
    	 @articobj = Article.find(@commobj.article_id)
     rescue ActiveRecord::RecordNotFound
        @articobj = nil
     end	
     
     	if @articobj.nil?
		  		   @artictitle = ""

		  else
		   @artictitle = @articobj.title
		  end

		if @articobj.published
	    @wsurln = Websitesinf.find(@articobj.websitesinf_id).url1			  
	    s_wsaddr = ""    
	      case @wsurln.downcase
	        when "www.deathtoglamour.com" : 
	              s_wsaddr = "http://84.234.17.41:3009/"+"backend/api"
	                                             
	        else 
	              s_wsaddr = ""
	        
	      end
	    if s_wsaddr != ""
	     blog = ActionWebService::Client::Soap.new(ArticleApi, s_wsaddr)
	 		 blog.upd_comm(@artictitle,@commobj.body,params[:record][:body])  
	 		end	      
    end
	end  
	
	 def destroy
	 		  @commobj = Comment.find(params[:id])
	      super

		 begin     
    	 @articobj = Article.find(@commobj.article_id)
     rescue ActiveRecord::RecordNotFound
        @articobj = nil
     end	
     
     	if @articobj.nil?
		  		   @artictitle = ""

		  else
		   @artictitle = @articobj.title
		  end

		if @articobj.published
	    @wsurln = Websitesinf.find(@articobj.websitesinf_id).url1			  
	    s_wsaddr = ""    
	      case @wsurln.downcase
	        when "www.deathtoglamour.com" : 
	              s_wsaddr = "http://84.234.17.41:3009/"+"backend/api"
	                                             
	        else 
	              s_wsaddr = ""
	        
	      end
	    if s_wsaddr != ""
	     blog = ActionWebService::Client::Soap.new(ArticleApi, s_wsaddr)
	 		 blog.del_comm(@artictitle,@commobj.body)  	 
	 		end     
    end	  

  end
end
